<?php

namespace app\common\model;

use think\Model;
use think\Db;

class TestModel extends Model
{
  protected $table = 'basic_test';
  protected $autoWriteTimestamp = true;
  protected $updateTime = 'update_time';
  private $_error;

  //获取文章列表
  public function getArticleList($params){
    $curPage = isset($params['page'])&&$params['page']>=1  ? $params['page']: 1;
    $pageSize = 12;
    $where['a.status'] = ['neq', 111];
    if (!empty($params)) {
      isset($params['category_id'])&&$params['category_id']!=-1 && $where['category_id'] = $params['category_id'];
      checkString($params['title']) && $where['title'] = ['like', "%".$params['title']."%"];
    }


    $totalRow = self::alias("a")->where($where)->count();
    $data = self::where($where)
    ->field("a.*,c.category_name")
    ->alias("a")
    ->join("basic_category c","c.id = a.category_id")
    ->order("a.id desc")
    ->page($curPage, $pageSize)
    ->select();
    $data = empty($data) ? [] : collection($data)->toArray();
    
    return [
        'pageSize' => $pageSize,
        'totalRow' => $totalRow,
        'curPage' => $curPage,
        'data' => $data
    ];
  }

  //获取分类下的文章
  public function getArticleByCategoryId($params){
    $curPage = isset($params['page'])&&$params['page']>=1  ? $params['page']: 1;
    $page_size = 12;

    $where = [
      'status' => 1,
      'category_id' => $params['category_id']
    ];

    return [
      'list' => Db::table($this->table)
      ->where($where)
      ->page($curPage, $page_size)
      ->order("show_time desc")
      ->select(),
      'page_size' => $page_size,
      'total' => Db::table($this->table)->where($where)->count(),
    ];
  }

  //获取文章详情
  public function getArticleById($id){
    $where = ['id'=>$id];
    $data = Db::table($this->table)->where($where)->find();

    return $data;
  }

  //删除
  public function deleteall($id)
  {
    return self::where("id", "=", $id)->update([
      'status' => 111,
      'update_time' => time()
    ]);
  }

}